import asyncio
import json

import tornado.web

from QualityInspectionViolation.Service.ViolationService import reviewAppeal, insertAppealService, queryMenu, callOut, \
    share, secondShare, queryDepartmentAdviser, roleSynchronization, menuSynchronization, roleSynchronizationAsync, \
    roleSynchronizationAsyncio, start


class ViolationHandler(tornado.web.RequestHandler):
    def post(self):
        appealId = self.get_body_argument("appealId")
        pn = self.get_body_argument("pn")

        self.set_header('Content-Type', 'application/json;charset=utf-8')
        self.write(json.dumps(reviewAppeal(appealId, pn)))

class AppealHandler(tornado.web.RequestHandler):
    def post(self):
        violationRecordId = self.get_body_argument("violationRecordId")
        customerId = self.get_body_argument("customerId")
        customerName = self.get_body_argument("customerName")
        adviserId = self.get_body_argument("adviserId")
        adviserName = self.get_body_argument("adviserName")

        self.set_header('Content-Type', 'application/json;charset=utf-8')
        self.write(json.dumps(insertAppealService(violationRecordId, customerId, customerName, adviserId, adviserName)))

class MenuHandler(tornado.web.RequestHandler):
    def post(self):
        adviserId = self.get_body_argument("adviserId")

        self.set_header('Content-Type', 'application/json;charset=utf-8')
        self.write(json.dumps(queryMenu(adviserId)))

class CallHandler(tornado.web.RequestHandler):
    def post(self):
        adviserId = self.get_body_argument("adviserId")

        self.set_header('Content-Type', 'application/json;charset=utf-8')
        self.write(json.dumps(callOut(adviserId)))

class ShareHandler(tornado.web.RequestHandler):
    def post(self):
        soundRecordingIdList = self.get_body_argument("soundRecordingIdList")
        organList = self.get_body_argument("organList")
        groupList = self.get_body_argument("groupList")
        adviserList = self.get_body_argument("adviserList")

        self.set_header('Content-Type', 'application/json;charset=utf-8')
        self.write(json.dumps(share(soundRecordingIdList, organList, groupList, adviserList)))

class SecondShareHandler(tornado.web.RequestHandler):
    def post(self):
        shareRecordingIdList = self.get_body_argument("shareRecordingIdList")

        self.set_header('Content-Type', 'application/json;charset=utf-8')
        self.write(json.dumps(secondShare(shareRecordingIdList)))

class RoleSynchronizationHandler(tornado.web.RequestHandler):
    def post(self):
        stayRoleSynchronizationIdList = self.get_body_argument("stayRoleSynchronizationIdList")

        self.set_header('Content-Type', 'application/json;charset=utf-8')
        self.write(json.dumps(roleSynchronization(stayRoleSynchronizationIdList)))

class RoleSynchronizationAsyncHandler(tornado.web.RequestHandler):
    def post(self):
        stayRoleSynchronizationIdList = self.get_body_argument("stayRoleSynchronizationIdList")

        self.set_header('Content-Type', 'application/json;charset=utf-8')
        self.write(json.dumps(roleSynchronizationAsync(stayRoleSynchronizationIdList)))

class MenuSynchronizationHandler(tornado.web.RequestHandler):
    def post(self):
        menuSynchronizationIdList = self.get_body_argument("menuSynchronizationIdList")

        self.set_header('Content-Type', 'application/json;charset=utf-8')
        self.write(json.dumps(menuSynchronization(menuSynchronizationIdList)))

class DepartmentHandler(tornado.web.RequestHandler):
    def post(self):
        organId = self.get_body_argument("organId")
        dimension = self.get_body_argument("dimension")
        position = self.get_body_argument("position")
        pageSize = self.get_body_argument("pageSize")
        pageNo = self.get_body_argument("pageNo")

        self.set_header('Content-Type', 'application/json;charset=utf-8')
        self.write(json.dumps(queryDepartmentAdviser(organId, dimension, position, pageSize, pageNo)))

class RoleSynchronizationAsyncioHandler(tornado.web.RequestHandler):
    def post(self):
        stayRoleSynchronizationIdList = self.get_body_argument("stayRoleSynchronizationIdList")

        self.set_header('Content-Type', 'application/json;charset=utf-8')
        self.write(json.dumps(asyncio.run(start(stayRoleSynchronizationIdList))))














